import json
from pathlib import Path

from llama_index.core.node_parser import MarkdownNodeParser
from llama_index.readers.file import FlatReader
from pydantic.v1 import BaseModel

if __name__ == "__main__":
    def show_json(data):
        """用于展示json数据"""
        if isinstance(data, str):
            obj = json.loads(data)
            print(json.dumps(obj, indent=4))
        elif isinstance(data, dict) or isinstance(data, list):
            print(json.dumps(data, indent=4))
        elif issubclass(type(data), BaseModel):
            print(json.dumps(data.dict(), indent=4, ensure_ascii=False))

    # 加载文档
    documents = FlatReader().load_data(file=Path("./llamaIndexDatas/ChatALL.md"))
    nodes = MarkdownNodeParser().get_nodes_from_documents(documents)
    show_json(nodes[2].json())